<?php
class MessageDao {
	public $table='message';
	public function add($data){
		ClassManager::getSql()->insert($this->table, $data);
	}
	
	public function searchList($where=array(),$order='',$limit=0){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->dataList1($this->table,'*',$where,$order,$limit);
	}
	
	public function searchNumber($where= array()){
		$where = $this->searchWhere($where);
		$tmp = ClassManager::getSql()->onedata1($this->table,"count(*)  ",$where);
		return $tmp[0];
	}
	
	public function searchMutilList($where=array(),$order,$limit){
		$where = $this->searchMutilWhere($where);
		$sql = " select a.*,b.email,b.phone from eo_message a left join eo_user b  ";
		$sql .= " on a.user_id=b.id  ";
		$sql .= " where 1=1  ";
		$sql .= $where;
		$sql .= $order;
		return ClassManager::getSql()->dataMutillist('user and message',$sql,$limit);
	}

	public function searchMutilNumber($where=array()){
		$where = $this->searchMutilWhere($where);
		$sql .= " select count(1) cnt from   ";
		$sql .= " (select a.id from eo_message a left join eo_user b  ";
		$sql .= " on a.user_id=b.id  ";
		$sql .= " where 1=1  ";
		$sql.= $where;		
		$sql.= " ) c ";		
		$tmp =  ClassManager::getSql()->dataMutillist('user and message_cnt ',$sql,$limit);
		return $tmp[0]['cnt'];
	}
	
	public function searchMutilWhere($where){
		$where1 = '';
		if(isset($where['add_time'])&&$where['add_time']>0){
			$where1 .=" and  a.add_time >='".$where['add_time']."'";
			$where['add_time'] = $where['add_time']+86399;
			$where1 .=" and  a.add_time <='".$where['add_time']."'";
		}
		if(isset($where['add_user'])&&$where['add_user']!==""){
			$where1 .=" and  (b.email like '%".$where['add_user']."%'";
			$where1 .=" or  b.phone like '%".$where['add_user']."%'";
			$where1 .=" )";
		}
		return $where1;
	}
		
	
	public function searchWhere($where){
		$where1 = array();
		foreach ($where as  $k=>$v){
			if (!empty($v)){
				$where1[$k] = $v;
			}
		}
		unset($where);
		return $where1;
	}
	public function get($id){
		return ClassManager::getSql()->oneDataCache($this->table, 'id', intval($id));
	}
}

?>